//
//  SmallDecisionsSingletonHelper.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/6/14.
//

#import "SmallDecisionsSingletonHelper.h"
#import "SmallDecisionsWebViewController.h"
#import "SmallDecisionsVipViewController.h"


#define USER_IS_VIP          @"USER_IS_VIP"
static SmallDecisionsSingletonHelper *instance = nil;

@implementation SmallDecisionsSingletonHelper

+ (SmallDecisionsSingletonHelper *)shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
#ifdef DEBUG
        [instance setIsVIP:YES];
#else
        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        BOOL isVIP = [userDefaults boolForKey:USER_IS_VIP];
        [instance setIsVIP:isVIP];
#endif
    });
    return instance;
}

- (void)setIsVIP:(BOOL)isVIP {
    _isVIP = isVIP;
    NSUserDefaults * userDefualt = [NSUserDefaults standardUserDefaults];
    [userDefualt setBool:isVIP forKey:USER_IS_VIP];
    [userDefualt synchronize];
}


+ (NSInteger)getUserNumberWithKey:(NSString *)key {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [[userDefaults objectForKey:key] intValue] ? : 1;
}

+ (void)setUserNumberWithKey:(NSString *)key number:(NSUInteger)number {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@(number) forKey:key];
    [userDefaults synchronize];
}

+ (void)openVipViewController {
    SmallDecisionsVipViewController *vipVC = [[SmallDecisionsVipViewController alloc] init];
    [SmallDecisionsNavigatorManager pushViewController:vipVC animated:YES];
}

+ (void)presentVipViewController {
    SmallDecisionsVipViewController *vipVC = [[SmallDecisionsVipViewController alloc] init];
    vipVC.showLeftClose = YES;
    [SmallDecisionsNavigatorManager sysPresentViewController:vipVC];
}

+ (void)openWebViewWithPath:(NSString *)path {
    SmallDecisionsWebViewController *webVC = [[SmallDecisionsWebViewController alloc] init];
    webVC.webUrl = [NSURL fileURLWithPath:path];
    [SmallDecisionsNavigatorManager sysPresentViewController:webVC];
}

+ (void)openUserProtocalWebView {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"userProtocol" ofType:@"html"];
    [SmallDecisionsSingletonHelper openWebViewWithPath:path];
}

+ (void)openPrivacyProtocalWebView {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"privacyProtocol" ofType:@"html"];
    [SmallDecisionsSingletonHelper openWebViewWithPath:path];
}

+ (void)openAboutUsWebView {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"aboutUs" ofType:@"html"];
    [SmallDecisionsSingletonHelper openWebViewWithPath:path];
}


+ (void)shareAppToPeopleWithText:(NSString *)text {
    [SmallDecisionsSingletonHelper shareToPeopleWithText:text path:@"https://apps.apple.com/cn/app/id6479500292"];
}


+ (void)shareAppToPeople {
    [SmallDecisionsSingletonHelper shareToPeopleWithPath:@"https://apps.apple.com/cn/app/id6749146656"];
}

+ (void)shareToPeopleWithPath:(NSString *)path {
    NSString *textToShare = @"聚会嗨-超好玩的互动破冰神器";
    [SmallDecisionsSingletonHelper shareToPeopleWithText:textToShare path:path];
}

+ (void)shareToPeopleWithText:(NSString *)text path:(NSString *)path {
    //在这里呢 如果想分享图片 就把图片添加进去  文字什么的通上
    NSString *urlStr = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *urlToShare = [NSURL URLWithString:urlStr];
    UIImage *imageToShare = [UIImage imageNamed:@"slogo"];
    NSArray *items = @[imageToShare,urlToShare,text];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
    if (@available(iOS 11.0, *)) {
        //UIActivityTypeMarkupAsPDF是在iOS 11.0 之后才有的
        activityVC.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypeOpenInIBooks, UIActivityTypeMarkupAsPDF];
    }else if (@available(iOS 9.0, *)){
        //UIActivityTypeOpenInIBooks是在iOS 9.0 之后才有的
        activityVC.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypeOpenInIBooks];
    }else{
        activityVC.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypeMail];
    }
    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
        
    };
    
    UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        activityVC.popoverPresentationController.sourceView = rootVC.view;
        [SmallDecisionsNavigatorManager sysPresentViewController:activityVC];
    }else{
        [SmallDecisionsNavigatorManager sysPresentViewController:activityVC];
    }
}


+ (UIWindow *)getKeyWindow {
    
    if ( @available(iOS 13.0, *) ) {
        for ( UIScene *scene in UIApplication.sharedApplication.connectedScenes ) {
            if ( [scene isKindOfClass:UIWindowScene.class] ) {
                return [(UIWindowScene *)scene windows].firstObject;
            }
        }
    }
    
    return [UIApplication sharedApplication].delegate.window;
}


@end
